{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "import visdom\n",
    "import PIL \n",
    "import numpy as np \n",
    "def show_image(image_path):\n",
    "    viz = visdom.Visdom(env='OCR_images')\n",
    "    image=PIL.Image.open(image_path)\n",
    "    print(\"原始大小:\",image.size)\n",
    "    iamge=image.convert('L')\n",
    "    viz.image( np.array(image ).transpose([2, 0, 1]))\n",
    "\n",
    "    \n",
    "    # 首先把w，h按照比例缩放成高度32的图片\n",
    "    w,h=image.size\n",
    "    \n",
    "    image=image.resize((int(w*32.0/h),32))\n",
    "    w = int(image.size[0] / (280 * 1.0 / 160))\n",
    "    \n",
    "    image = image.resize((w,32), PIL.Image.BILINEAR)\n",
    "    viz.image( np.array(image ).transpose([2, 0, 1]))\n",
    "    print(\"改变后的大小:\",image.size)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Setting up a new session...\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始大小: (754, 145)\n",
      "改变后的大小: (94, 32)\n"
     ]
    }
   ],
   "source": [
    "image_path=\"/home/wudeyang/data/OCR/test/test11.jpg\" \n",
    "show_image(image_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 保存测试图片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始大小: (990, 126)\n",
      "改变后的大小: (251, 32)\n"
     ]
    }
   ],
   "source": [
    "image=PIL.Image.open(image_path)\n",
    "print(\"原始大小:\",image.size)\n",
    "iamge=image.convert('L')\n",
    "\n",
    "# 首先把w，h按照比例缩放成高度32的图片\n",
    "w,h=image.size\n",
    "\n",
    "image=image.resize((int(w*32.0/h),32), PIL.Image.BILINEAR)\n",
    "\n",
    "\n",
    "print(\"改变后的大小:\",image.size)\n",
    "image.save('/home/wudeyang/data/OCR/test/test0_1.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 双向校验图片和标签是否对的上"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "with open('/home/wudeyang/data/OCR/test.txt') as f:\n",
    "    for line in f.readlines():\n",
    "        image_name=line.split()[0]\n",
    "        if not os.path.exists(\"/home/wudeyang/data/OCR/images/\"+image_name):\n",
    "            print(image_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "364400\n",
      "3644006\n"
     ]
    }
   ],
   "source": [
    "images_name=os.listdir(\"/home/wudeyang/data/OCR/images/\")\n",
    "images_list=[]\n",
    "\n",
    "with open('/home/wudeyang/data/OCR/test.txt') as f:\n",
    "    for line in f.readlines():\n",
    "        image_name=line.split()[0]\n",
    "        images_list.append(image_name)\n",
    "print(len(images_list))\n",
    "with open('/home/wudeyang/data/OCR/train.txt') as f:\n",
    "    for line in f.readlines():\n",
    "        image_name=line.split()[0]\n",
    "        images_list.append(image_name)\n",
    "print(len(images_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [],
   "source": [
    "dic={}\n",
    "for i in images_list:\n",
    "    dic[i]=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "72728656_2205086079.jpg\n"
     ]
    }
   ],
   "source": [
    "for image_name in images_name:\n",
    "    if image_name not in dic:\n",
    "        print(image_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python (pytorch)",
   "language": "python",
   "name": "pytorch"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
